using System;
using System.Linq.Expressions;

namespace OS.Infrastructure.Domain
{
    public class SpecificationNegate<T> : Specification<T> where T : class
    {
        private readonly Specification<T> spec;

        public SpecificationNegate(Specification<T> spec)
        {
            this.spec = spec;
        }

        protected override object[] Parameters
        {
            get { return new[] {spec}; }
        }

        public override Expression<Func<T, bool>> IsSatisfiedBy()
        {
            ParameterExpression param = Expression.Parameter(typeof (T), "x");
            return Expression.Lambda<Func<T, bool>>(
                Expression.Not(spec.IsSatisfiedBy().Body), spec.IsSatisfiedBy().Parameters);
        }
    }
}